package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.SmsService;
import com.itheima.health.vo.SmsLoginParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("mobile/login")
public class LoginController {

    @DubboReference
    private SmsService smsService;
    @DubboReference
    private MemberService memberService;

    @PostMapping("smsLogin")
    public Result smsLogin(@RequestBody SmsLoginParam param){
        boolean code = smsService.checkValidateCode("MOBILE_SIGNIN", param.getTelephone(), param.getValidateCode());
        if (!code){//如果验证码不正确
            //直接返回false
            return new Result(false, MessageConst.VALIDATECODE_ERROR);
        }
        //创建用户对象
        Member member = new Member();
        //传入手机号
        member.setPhoneNumber(param.getTelephone());
        //根据手机号更新或新增用户 将返回结果赋值给用户对象
        Member insertOrUpdate = memberService.insertOrUpdate(member);
        log.info("登录成功 - {}",insertOrUpdate.getId());

        return new Result(true,MessageConst.LOGIN_SUCCESS);
    }


}
